package com.example.xinyun.init;

import com.example.xinyun.db.TbXinli;
import com.example.xinyun.db.db_xinli;
import com.example.xinyun.provider.XinliProvider;
import com.google.gson.Gson;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.ListContainer;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

public class InitXinli {
    private  List<TbXinli> xinliLists=new ArrayList();
    private  XinliProvider xinliProvider;
    public List<TbXinli> getXinliLists() {
        return xinliLists;
    }
    public  void Refresh(String url, Context context, AbilitySlice abilitySlice, ListContainer listContainer){
        ZZRHttp.get(url, new ZZRCallBack.CallBackString() {

            @Override
            //http访问出错，此部分在主线程中工作,可以更新UI等操做。
            public void onFailure(int code, String errorMessage) {
                new ToastDialog(context)
                        .setText("加载失败，请检查网络连接！")
                        .show();
            }
            @Override
            //http访问成功，此部分在主线程中工作，可以更新UI等操作。
            public  void onResponse(String response) {
                //将http请求返回的json字符串转换成一个dbPsychological类的对象
                db_xinli xinli=new Gson().fromJson(response, db_xinli.class);
                if (xinli!=null)
                {
                    List<TbXinli> data = xinli.getData();
                    //清空xinliLists中的数据
                    xinliLists.clear();
                    //将获取到的数据全部添加到xinliLists里面
                    xinliLists.addAll(data);
                    //对newsProvider进行更新
                    xinliProvider=new XinliProvider(xinliLists, (AbilitySlice) abilitySlice);
                    listContainer.setItemProvider(xinliProvider);
                }
            }
        });
    }
}
